package com.yyaccp.appinfo.controller;

import com.yyaccp.appinfo.domain.*;
import com.yyaccp.appinfo.exception.LoginException;
import com.yyaccp.appinfo.service.*;
import com.yyaccp.appinfo.utils.AjaxResult;
import com.yyaccp.appinfo.utils.Constants;
import com.yyaccp.appinfo.utils.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/backend")
public class BackendUserController {

    @Autowired
    private BackendUserService backendUserService;
    @Autowired
    private DataDictionaryService dataDictionaryService;
    @Autowired
    private AppCategoryService appCategoryService;
    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private AppVersionService appVersionService;

    @PostMapping("/login")
    public AjaxResult login(BackendUser backendUser, HttpSession session){
        try{
            BackendUser user = backendUserService.backendLogin(backendUser);
            user.setUserPassword("******");
            session.setAttribute(Constants.USER_SESSION,user);
            return AjaxResult.success(user);
        }catch(LoginException ex){
            return AjaxResult.error(ex.getMessage());
        }
    }

    @RequestMapping("/out")
    public AjaxResult out(HttpSession session){
        session.invalidate();
        return AjaxResult.success();
    }

    @RequestMapping("/selectDataDictionary")
    public AjaxResult selectDataDictionary(String typeName){
        List<DataDictionary> list = dataDictionaryService.selectTypeName(typeName);
        if(list != null){
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error("查询时出现错误");
        }
    }

    @RequestMapping("/selectParendId")
    public AjaxResult selectParendId(@RequestParam(defaultValue = "1")Integer parentId){
        List<AppCategory> list = appCategoryService.selectParentId(parentId);
        if(list != null){
            return AjaxResult.success(list);
        }else{
            return AjaxResult.error("查询一级分类时出现错误!");
        }
    }

    @RequestMapping("/selectAppInfoPage")
    public AjaxResult selectAppInfoPage(AppInfo appinfo, @RequestParam(value = "page") Integer pageNo,@RequestParam(value = "limit") Integer pageSize){
        Page page = new Page();
        page.setCurrPage(pageNo);
        page.setPageSize(pageSize);
        appInfoService.AppInfoPage(appinfo,page);
       /* List<Object> list = new ArrayList<Object>();
        list.add(page);*/
     /*   Map<String, Object> map = new HashMap<>();
       *//* "code": 0,
                "msg": "",
                "count": 1000,
                "data": [{}, {}]*//*
        map.put("code", 0);
        map.put("msg","success");
        map.put("count", page.getTotalCount());
        map.put("data", page.getList());
        return map;*/
        return AjaxResult.success(page);
    }

    @RequestMapping("/selectByPrimaryKey")
    public AjaxResult selectByPrimaryKey( AppVersion version){
        AppVersion appVersion = appVersionService.selectByPrimaryKey(version);
        if(appVersion != null){
            return AjaxResult.success(appVersion);
        }else{
            return AjaxResult.error("查询版本号时出现异常!");
        }
    }

    @RequestMapping("/updateStatusById")
    public AjaxResult updateStatusById(AppInfo appInfo){
        if(appInfoService.updateStatusById(appInfo)>0){
            return AjaxResult.success("操作成功!");
        }else{
            return AjaxResult.error("操作失败!");
        }
    }
}
